local kuangque = fk.CreateSkill{
  name = "hanqing__kuangque",
}

Fk:loadTranslationTable{
  ["hanqing__kuangque"] = "匡阙",
  [":hanqing__kuangque"] = "其他角色回合结束时，若中央区尚缺花色，你可以依次用场上牌、你手牌、牌堆顶与其拼点。"..
  "若因你赢且补完花色，你用中央区将其手牌数调整至全场最大（至多获得5张）；若你没赢，其可以视为对你使用【杀】。",

  ["#hanqing__kuangque-invoke"] = "匡阙：是否对 %dest 发动“匡阙”，与其进行三次拼点？",
  ["#hanqing__kuangque1-invoke"] = "匡阙：请选择场上一张牌作为拼点牌",
  ["#hanqing__kuangque-ask"] = "匡阙：令 %dest 获得中央区的至少%arg张牌",
  ["#hanqing__kuangque-slash"] = "匡阙：是否视为对 %src 使用【杀】？",
}

local function suitCheck(room)
  local suits = {}
  for _, id in ipairs(room:getBanner("@$CenterArea")) do
    table.insertIfNeed(suits, Fk:getCardById(id).suit)
  end
  return #suits < 4
end

local function doKuangque(player, target, pindian, suit_check)
  local room = player.room
  if pindian.results[target].winner == player then
    if suit_check and #room:getBanner("@$CenterArea") > 0 then
      local nums = table.map(room.alive_players, function (p)
        return p:getHandcardNum()
      end)
      local max = math.max(table.unpack(nums))
      local n
      if target:getHandcardNum() >= max then
        n = 0
      else
        n = max - target:getHandcardNum()
        if n > math.min(5, #room:getBanner("@$CenterArea")) then
          n = nil
        end
      end
      if n then
        local ids = room:askToChooseCards(player, {
          target = player,
          min = n,
          max = 5,
          flag = { card_data = {{ "@$CenterArea", room:getBanner("@$CenterArea") }} },
          skill_name = kuangque.name,
          prompt = "#hanqing__kuangque-ask::"..target.id..":"..n,
        })
        if #ids > 0 then
          room:moveCardTo(ids, Card.PlayerHand, target, fk.ReasonJustMove, kuangque.name, nil, true, player)
        end
      end
    end
  else
    if player:hasSkill("hanqing__jigou") and target.hp > player.hp and
      target:getHandcardNum() > player:getHandcardNum() and
      room:askToSkillInvoke(player, {
        skill_name = "hanqing__jigou",
        prompt = "#hanqing__jigou-invoke::"..target.id,
      }) then
      room:useVirtualCard("slash", nil, player, target, kuangque.name, true)
    elseif target:canUseTo(Fk:cloneCard("slash"), player, { bypass_distances = true, bypass_times = true }) and
      room:askToSkillInvoke(target, {
        skill_name = kuangque.name,
        prompt = "#hanqing__kuangque-slash:"..player.id,
      }) then
      room:useVirtualCard("slash", nil, target, player, kuangque.name, true)
    end
  end
  room:delay(1000)
end

kuangque:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return target ~= player and player:hasSkill(kuangque.name) and
      suitCheck(player.room) and not target.dead and player:canPindian(target, true)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = kuangque.name,
      prompt = "#hanqing__kuangque-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local suit_check = suitCheck(room)
    local cards = {}
    for _, p in ipairs(room:getAlivePlayers()) do
      table.insertTable(cards, p:getCardIds("ej"))
    end
    if #cards > 0 then
      cards = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = kuangque.name,
        pattern = tostring(Exppattern{ id = cards }),
        prompt = "#hanqing__kuangque1-invoke",
        cancelable = false,
        expand_pile = table.filter(cards, function (id)
          return not table.contains(player:getCardIds("e"), id)
        end),
      })
      local pindian = player:pindian({target}, kuangque.name, Fk:getCardById(cards[1]))
      if player.dead or target.dead then return end
      doKuangque(player, target, pindian, suitCheck(room))
      if player.dead or target.dead then return end
    end
    if player:canPindian(target) then
      room:doIndicate(player, {target})
      suit_check = suitCheck(room)
      local pindian = player:pindian({target}, kuangque.name)
      suit_check = not suit_check and suitCheck(room)
      if player.dead or target.dead then return end
      doKuangque(player, target, pindian, suit_check)
      if player.dead or target.dead then return end
    end
    if player:canPindian(target, true) then
      room:doIndicate(player, {target})
      suit_check = suitCheck(room)
      local pindian = player:pindian({target}, kuangque.name, Fk:getCardById(room:getNCards(1)[1]))
      suit_check = not suit_check and suitCheck(room)
      if player.dead or target.dead then return end
      doKuangque(player, target, pindian, suit_check)
    end
  end,
})

kuangque:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return kuangque
